package org.chainsale.main;

import java.awt.BorderLayout;
import java.awt.GridLayout;
import java.util.ArrayList;
import java.util.List;

import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JSplitPane;
import javax.swing.JTextField;

import org.chainsale.action.AddNodeActionListener;
import org.chainsale.bean.ActiveItem;
import org.chainsale.bean.Practitioner;
import org.chainsale.prefuse.graph.FuseTreePanel;

import prefuse.util.ui.UILib;

public class TreePanelFrame extends JFrame {

	private static final long serialVersionUID = 1L;

	private List<JLabel> propsLabel = new ArrayList<JLabel>();

	private JTextField addedNumText;
	private JButton addButton;
	private JTextField addedNameText;
	
	
	private JLabel totalPractionerText;
	private JLabel totalOutgoingText;
	private JLabel totalIncomingText;
	private JLabel inOutPercentText;
	
	public void init() {
		
		int width = 800, height = 600;
		int leftWidth = 600, rightWidth = 200;
		
		setLocation(300, 100);
		setSize(width, height);
		
		UILib.setPlatformLookAndFeel();
		
		ActiveItem instance = ActiveItem.getInstance();
		
		final JPanel systemInfoPane = createSystemInfoPane(180, 40);
		final JPanel propertiesPan = createPropertiesPane(280, 350);
		final JPanel editPropPan = createEditPropertiesPane(180, 160);
		
		
		instance.setPropLabels(propsLabel);
		instance.setAddedNameText(addedNameText);
		instance.setAddedNumText(addedNumText);
		instance.addSystemPanel(totalPractionerText, totalOutgoingText, totalIncomingText, inOutPercentText);
		
		FuseTreePanel tPanel = new FuseTreePanel();
		tPanel.setSize(leftWidth, height);
		
		addActionListener4AddButton(tPanel);
		
		instance.changeNodeProperties();
		instance.changeSystemInfo();
		
		// create a panel for editing force values
        final JPanel right = new JPanel(new GridLayout(3, 1));
        right.setSize(rightWidth, height);
        
        
        right.add(systemInfoPane);
//        right.add(createLabel("-------------------------", 200, 20));
        right.add(propertiesPan);
//        right.add(createLabel("-------------------------", 200, 20));
        right.add(editPropPan);
        
		
		// create a new JSplitPane to present the interface
        JSplitPane split = new JSplitPane();
        split.setLeftComponent(tPanel);
        split.setRightComponent(right);
        
		split.setSize(width, height);
        
		add(split, BorderLayout.CENTER);
		
		setVisible(true);
		
        split.setDividerLocation(0.75d);
	}
	
	public JPanel createSystemInfoPane(int w, int h) {
		int rows = 4, cols = 2;
		JPanel sInfoPane = new JPanel(new GridLayout(rows, cols, 5, 10));
		sInfoPane.setSize(w, h);
		sInfoPane.setVisible(true);
		
		int wx1 = (w - 5 * cols) / 3 ; int wx2 = (w - 5 * cols) - wx1; int hy = (h - 10 * rows) / rows;
		
		// system number
		JLabel totalPractionerLabel = createLabel("系统总人数: ", wx1, hy);
        totalPractionerText = createLabel(wx2, hy);
        sInfoPane.add(totalPractionerLabel); sInfoPane.add(totalPractionerText);
        
        JLabel totalOutgoingLabel = createLabel("系统总进账: ", wx1, hy);
        totalOutgoingText = createLabel(wx2, hy);
        sInfoPane.add(totalOutgoingLabel); sInfoPane.add(totalOutgoingText);
        
        JLabel totalIncomingLabel = createLabel("系统总支出: ", wx1, hy);
        totalIncomingText = createLabel(wx2, hy);
        sInfoPane.add(totalIncomingLabel); sInfoPane.add(totalIncomingText);
        
        JLabel inOutPercentLabel = createLabel("支出 / 进账: ", wx1, hy);
        inOutPercentText = createLabel(wx2, hy);
        sInfoPane.add(inOutPercentLabel); sInfoPane.add(inOutPercentText);
        
		return sInfoPane;
	}
	
	public JPanel createPropertiesPane(int w, int h) {
		int rows = 7, cols = 2;
		JPanel propertiesPan = new JPanel(new GridLayout(rows, cols, 5, 10));
//		propertiesPan.setPreferredSize(new Dimension(w, h));
		propertiesPan.setSize(w, h);
		propertiesPan.setVisible(true);
		
		int wx1 = (w - 5 * cols) / 3 ; int wx2 = (w - 5 * cols) - wx1; int hy = (h - 10 * rows) / rows;
		
		// panel title
        JLabel titleLabel = createLabel("当前业务员信息", wx1, hy);
        JLabel titleText = createLabel(wx2, hy);
        propertiesPan.add(titleLabel); propertiesPan.add(titleText); 
		
        // name
        JLabel nameLabel = createLabel("姓名: ", wx1, hy);
        JLabel nameText = createLabel(wx2, hy);
        propertiesPan.add(nameLabel); propertiesPan.add(nameText); 

        // share
        JLabel shareLabel = createLabel("份额: ", wx1, hy);
        JLabel shareText = createLabel(wx2, hy);
        propertiesPan.add(shareLabel); propertiesPan.add(shareText);
        
        // outgoing
        JLabel outgoingLabel = createLabel("支出: ", wx1, hy);
        JLabel outgoingText = createLabel(wx2, hy);
        propertiesPan.add(outgoingLabel); propertiesPan.add(outgoingText); 
        
        // incoming
        JLabel incomingLabel = createLabel("收入: ", wx1, hy);
        JLabel incomingText = createLabel(wx2, hy);
        propertiesPan.add(incomingLabel); propertiesPan.add(incomingText); 
        
        // rank
        JLabel rankLabel = createLabel("级别: ", wx1, hy);
        JLabel rankText = createLabel(wx2, hy);
        propertiesPan.add(rankLabel); propertiesPan.add(rankText); 
        
        // superior
        JLabel superiorLabel = createLabel("推荐人: ", wx1, hy);
        JLabel superiorText = createLabel(wx2, hy);
        propertiesPan.add(superiorLabel); propertiesPan.add(superiorText);
        
        propsLabel.add(nameText); propsLabel.add(shareText); propsLabel.add(outgoingText);
        propsLabel.add(incomingText); propsLabel.add(rankText); propsLabel.add(superiorText);
        
        return propertiesPan;
	}
	
	public JPanel createEditPropertiesPane(int w, int h) {
		int rows = 4;
		JPanel editPropertiesPan = new JPanel(new GridLayout(rows, 2, 5, 10));
		editPropertiesPan.setSize(w, h);
		editPropertiesPan.setVisible(true);
		
		int wx1 = w / 4 ; int wx2 = w - wx1; int hy = h / rows;
		
		// title
        JLabel titleLabel = createLabel("新增业务员", wx1, hy);
        JLabel titleText = createLabel("", wx2, hy);
        editPropertiesPan.add(titleLabel); editPropertiesPan.add(titleText); 
		
        // name
        JLabel addedNameLabel = createLabel("姓名: ", wx1, hy);
        addedNameText = createTextField(wx2, hy);
        editPropertiesPan.add(addedNameLabel); editPropertiesPan.add(addedNameText); 

        // share
        JLabel shareLabel = createLabel("份额: ", wx1, hy);
        JTextField shareText = createTextField(wx2, hy);
        editPropertiesPan.add(shareLabel); editPropertiesPan.add(shareText);
        
        // button
        addButton = createButton("增加", wx1, hy);
        addedNumText = createTextField(wx2, hy);
        addedNumText.setText("1");
        editPropertiesPan.add(addButton); editPropertiesPan.add(addedNumText);
        
        return editPropertiesPan;
	}
	
	private void addActionListener4AddButton(FuseTreePanel tPanel) {
		addButton.addActionListener(new AddNodeActionListener(tPanel));
	}
	
	public JLabel createLabel(String labelTxt, int w, int h) {
		JLabel label = new JLabel(labelTxt);
        label.setSize(w, h);
//		label.setPreferredSize(new Dimension(w, h));
        return label;
	}
	
	public JLabel createLabel(int w, int h) {
        return createLabel("", w, h);
	}

	public JTextField createTextField(int w, int h) {
		JTextField textField = new JTextField("");
		textField.setSize(w, h);
//		textField.setPreferredSize(new Dimension(w, h));
		return textField;
	}
	
	public JButton createButton(String txt, int w, int h) {
		JButton button = new JButton(txt);
        button.setSize(w, h);
//		button.setPreferredSize(new Dimension(w, h));
        return button;
	}
	
	public static void main(String[] args) {
		TreePanelFrame tpt = new TreePanelFrame();
		tpt.init();
		tpt.setTitle("Chain Sale");
		tpt.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);      
	}
	
}
